package net.jcip.examples.chapter4;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * 破坏线程安全性的引用逸出
 */

public class Escape {

    public static final Map<String,Point> locations = new HashMap<String,Point>(){
        {
        put("car",new Point(23,25));
        put("air",new Point(10,20));
        }

        @Override  //注解，加上@Override可以让编译器帮助检查是否进行了正确的覆写。希望进行覆写，但是不小心写错了方法签名，编译器会报错。
        public String toString() {
            StringBuffer result = new StringBuffer();
            Set<String> keySet = locations.keySet();
            Iterator<String> iterator = keySet.iterator();
            while(iterator.hasNext()){
                String key = iterator.next();
                result.append("key:").
                        append(key + " ").
                        append("value:").
                        append(locations.get(key) + " ");
            }
            return result.toString();
        }


    };

}
